//AHT20 IIC通信，应当包含IIC.H

#include "AHT20.H"
u8 CRC_WrongFlag=0;//CRC错误标志位   1:Wrong,0:True
u8  CTDATA[7]={0};//用于CRC传递数组

u8 AHT20_Read_Status(void)//读取AHT20的状态寄存器
{
	u8 Byte_first;
	I2C_Start();
	Write_IIC_Byte(0x71);//地址
//	Receive_ACK();//检测是否应答
	Byte_first = Read_IIC_Byte();
//	Send_NOT_ACK();
	I2C_Stop();
	return Byte_first;
}

u8 AHT20_Read_Cal_Enable(void)  //查询cal enable位有没有使能
{
	u8 val = 0;//ret = 0,
  val = AHT20_Read_Status();
	 if((val & 0x68)==0x08)
		 return 1;
   else  return 0;
 }

void AHT20_SendAC(void) //向AHT20发送AC命令
{

	I2C_Start();
	Write_IIC_Byte(0x70);
//	Receive_ACK();
	Write_IIC_Byte(0xac);//0xAC采集命令
//	Receive_ACK();
	Write_IIC_Byte(0x33);
//	Receive_ACK();
	Write_IIC_Byte(0x00);
//	Receive_ACK();
	I2C_Stop();

}



/*****************/

//CRC校验类型：CRC8/MAXIM
//多项式：X8+X5+X4+1
//Poly：0011 0001  0x31
//高位放到后面就变成 1000 1100 0x8c
//C现实代码：
u8 Calc_CRC8(u8 *message,u8 Num)
{
        u8 i;
        u8 byte;
        u8 crc=0xFF;
  for(byte=0; byte<Num; byte++)
  {
    crc^=(message[byte]);
    for(i=8;i>0;--i)
    {
      if(crc&0x80) crc=(crc<<1)^0x31;
      else crc=(crc<<1);
    }
  }
        return crc;
}



void AHT20_Read_CTdata(u32 *ct) //没有CRC校验，直接读取AHT20的温度和湿度数据
{
	volatile u8  Byte_1th=0;
	volatile u8  Byte_2th=0;
	volatile u8  Byte_3th=0;
	volatile u8  Byte_4th=0;
	volatile u8  Byte_5th=0;
	volatile u8  Byte_6th=0;
	volatile u8  Byte_7th=0;
	 u32 RetuData = 0;
	u16 cnt = 0;
	AHT20_SendAC();//向AHT10发送AC命令
	Delay100ms();//延时100ms左右	
    cnt = 0;
	while(((AHT20_Read_Status()&0x80)==0x80))//直到状态bit[7]为0，表示为空闲状态，若为1，表示忙状态
	{
		Delay10ms();
		if(cnt++>=100)
		{
		 break;
		 }
	}
	I2C_Start();
	Write_IIC_Byte(0x71);
	Receive_ACK();
	CTDATA[0]=Byte_1th = Read_IIC_Byte();//状态字，查询到状态为0x98,表示为忙状态，bit[7]为1；状态为0x1C，或者0x0C，或者0x08表示为空闲状态，bit[7]为0
	Send_ACK();
	CTDATA[1]=Byte_2th = Read_IIC_Byte();//湿度
	Send_ACK();
	CTDATA[2]=Byte_3th = Read_IIC_Byte();//湿度
	Send_ACK();
	CTDATA[3]=Byte_4th = Read_IIC_Byte();//湿度/温度
	Send_ACK();
	CTDATA[4]=Byte_5th = Read_IIC_Byte();//温度
	Send_ACK();
	CTDATA[5]=Byte_6th = Read_IIC_Byte();//温度
	Send_NOT_ACK();
	I2C_Stop();

	RetuData = (RetuData|Byte_2th)<<8;
	RetuData = (RetuData|Byte_3th)<<8;
	RetuData = (RetuData|Byte_4th);
	RetuData =RetuData >>4;
	ct[0] = RetuData;//湿度
	RetuData = 0;
	RetuData = (RetuData|Byte_4th)<<8;
	RetuData = (RetuData|Byte_5th)<<8;
	RetuData = (RetuData|Byte_6th);
	RetuData = RetuData&0xfffff;
	ct[1] =RetuData; //温度
	

}


void AHT20_Read_CTdata_crc(u32 *ct) //CRC校验后，读取AHT20的温度和湿度数据
{
	volatile u8  Byte_1th=0;
	volatile u8  Byte_2th=0;
	volatile u8  Byte_3th=0;
	volatile u8  Byte_4th=0;
	volatile u8  Byte_5th=0;
	volatile u8  Byte_6th=0;
	volatile u8  Byte_7th=0;
	 u32 RetuData = 0;
	 u16 cnt = 0;

	
	AHT20_SendAC();//向AHT10发送AC命令
	Delay100ms();//延时100ms左右	
    cnt = 0;
	while(((AHT20_Read_Status()&0x80)==0x80))//直到状态bit[7]为0，表示为空闲状态，若为1，表示忙状态
	{
		Delay1ms();
		if(cnt++>=100)
		{
		 break;
		}
	}
	
	I2C_Start();
	Write_IIC_Byte(0x71);
//	Receive_ACK();
	CTDATA[0]=Byte_1th = Read_IIC_Byte();//状态字，查询到状态为0x98,表示为忙状态，bit[7]为1；状态为0x1C，或者0x0C，或者0x08表示为空闲状态，bit[7]为0
	Send_ACK();
	CTDATA[1]=Byte_2th = Read_IIC_Byte();//湿度
	Send_ACK();
	CTDATA[2]=Byte_3th = Read_IIC_Byte();//湿度
	Send_ACK();
	CTDATA[3]=Byte_4th = Read_IIC_Byte();//湿度/温度
	Send_ACK();
	CTDATA[4]=Byte_5th = Read_IIC_Byte();//温度
	Send_ACK();
	CTDATA[5]=Byte_6th = Read_IIC_Byte();//温度
	Send_ACK();
	CTDATA[6]=Byte_7th = Read_IIC_Byte();//CRC数据
	Send_NOT_ACK();                           //注意: 最后是发送NAK
	I2C_Stop();
	
	if(Calc_CRC8(CTDATA,6)==Byte_7th)
	{
	CRC_WrongFlag=0;
		
	RetuData = (RetuData|Byte_2th)<<8;
	RetuData = (RetuData|Byte_3th)<<8;
	RetuData = (RetuData|Byte_4th);
	RetuData =RetuData >>4;
	ct[0] = RetuData;//湿度
	RetuData = 0;
	RetuData = (RetuData|Byte_4th)<<8;
	RetuData = (RetuData|Byte_5th)<<8;
	RetuData = (RetuData|Byte_6th);
	RetuData = RetuData&0xfffff;
	ct[1] =RetuData; //温度
		
	}
	else
	{
//		ct[0]=0x00;
//		ct[1]=0x00;//校验错误返回值，客户可以根据自己需要更改
		CRC_WrongFlag=1;//CRC错误标志位

	}//CRC数据
}


void AHT20_Init(void)   //初始化AHT20
{	
	I2C_Start();
	Write_IIC_Byte(0x70);
//	Receive_ACK();
	Write_IIC_Byte(0xa8);//0xA8进入NOR工作模式
//	Receive_ACK();
	Write_IIC_Byte(0x00);
//	Receive_ACK();
	Write_IIC_Byte(0x00);
//	Receive_ACK();
	I2C_Stop();

	Delay10ms();//延时10ms左右

	I2C_Start();
	Write_IIC_Byte(0x70);
//	Receive_ACK();
	Write_IIC_Byte(0xbe);//0xBE初始化命令，AHT20的初始化命令是0xBE,   AHT10的初始化命令是0xE1
//	Receive_ACK();
	Write_IIC_Byte(0x08);//相关寄存器bit[3]置1，为校准输出
//	Receive_ACK();
	Write_IIC_Byte(0x00);
//	Receive_ACK();
	I2C_Stop();
	Delay10ms();//延时10ms左右
}
void JH_Reset_REG(u8 addr)
{
	
	u8 Byte_first,Byte_second,Byte_third;
	I2C_Start();
	Write_IIC_Byte(0x70);//原来是0x70
//	Receive_ACK();
	Write_IIC_Byte(addr);
//	Receive_ACK();
	Write_IIC_Byte(0x00);
//	Receive_ACK();
	Write_IIC_Byte(0x00);
//	Receive_ACK();
	I2C_Stop();

	Delay10ms();//延时10ms左右
	I2C_Start();
	Write_IIC_Byte(0x71);//
//	Receive_ACK();
	Byte_first = Read_IIC_Byte();
	Send_ACK();
	Byte_second = Read_IIC_Byte();
	Send_ACK();
	Byte_third = Read_IIC_Byte();
//	Send_NOT_ACK();
	I2C_Stop();
	
  Delay10ms();//延时10ms左右
	I2C_Start();
	Write_IIC_Byte(0x70);///
//	Receive_ACK();
	Write_IIC_Byte(0xB0|addr);////寄存器命令
//	Receive_ACK();
	Write_IIC_Byte(Byte_second);
//	Receive_ACK();
	Write_IIC_Byte(Byte_third);
//	Receive_ACK();
	I2C_Stop();
	
	Byte_second=0x00;
	Byte_third =0x00;
}

void AHT20_Start_Init(void)
{
	JH_Reset_REG(0x1b);
	JH_Reset_REG(0x1c);
	JH_Reset_REG(0x1e);
}




